package br.com.lucasddaniel.inbuzios.adapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import br.com.lucasddaniel.inbuzios.R;
import br.com.lucasddaniel.inbuzios.activities.GeracaoImpressaoPasseioActivity;
import br.com.lucasddaniel.inbuzios.bean.PacoteArquivoBean;
import br.com.lucasddaniel.inbuzios.bean.TuristaBean;

public class PacotesContratadosAdapter extends ArrayAdapter<PacoteArquivoBean> {
    private GeracaoImpressaoPasseioActivity act = null;
	private List<PacoteArquivoBean> listPacotesBean = null;
	private TuristaBean turistaBean = null;

	public PacotesContratadosAdapter(GeracaoImpressaoPasseioActivity act,
			int resource, List<PacoteArquivoBean> listPacotesBean,
			TuristaBean tb) {
		super(act, resource, listPacotesBean);
		this.act = act;
		this.listPacotesBean = listPacotesBean;
		this.turistaBean = tb;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) act
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.row_pacotes_contratados,
				parent, false);
		if (listPacotesBean != null && listPacotesBean.size() != 0) {
			final PacoteArquivoBean pacote = listPacotesBean.get(position);
			if (pacote != null) {

				final Button btnDataPasseio = (Button) rowView
						.findViewById(R.id_row_pacotes_contratados.btn_data_passeio);
				final Calendar cDataHora = Calendar.getInstance();
				if (pacote.getDataHoraSaida() != null) {
					cDataHora.setTime(pacote.getDataHoraSaida());
				}
				final SimpleDateFormat sdfData = new SimpleDateFormat("dd/MM/yy");
				final int ano = cDataHora.get(Calendar.YEAR);
				final int mes = cDataHora.get(Calendar.MONTH);
				final int dia = cDataHora.get(Calendar.DAY_OF_MONTH);
				btnDataPasseio.setText(sdfData.format(cDataHora.getTime()));
				btnDataPasseio.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						DatePickerDialog dpd = new DatePickerDialog(act,
								new DatePickerDialog.OnDateSetListener() {
									public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
										cDataHora.set(Calendar.DAY_OF_MONTH, dayOfMonth);
										cDataHora.set(Calendar.MONTH, monthOfYear);
										cDataHora.set(Calendar.YEAR, year);
										pacote.setDataHoraSaida(cDataHora.getTime());
//										btnDataPasseio.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
										btnDataPasseio.setText(sdfData.format(cDataHora.getTime()));
									}
								}, ano, mes, dia);
						dpd.show();
					}
				});

				final Button btnHoraPasseio = (Button) rowView
						.findViewById(R.id_row_pacotes_contratados.btn_hora_passeio);
				final SimpleDateFormat sdfHora = new SimpleDateFormat("HH:mm");
				final int hora = cDataHora.get(Calendar.HOUR_OF_DAY);
				final int minuto = cDataHora.get(Calendar.MINUTE);
				btnHoraPasseio.setText(sdfHora.format(cDataHora.getTime()));
				btnHoraPasseio.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						TimePickerDialog tpd = new TimePickerDialog(act,
								new TimePickerDialog.OnTimeSetListener() {
									public void onTimeSet(TimePicker view, int hour, int minute) {
										cDataHora.set(Calendar.HOUR_OF_DAY, hour);
										cDataHora.set(Calendar.MINUTE, minute);
										pacote.setDataHoraSaida(cDataHora.getTime());
										btnHoraPasseio.setText(sdfHora.format(cDataHora.getTime()));
									}
								}, hora, minuto, true);
						tpd.show();
					}
				});
				TextView txtViewNome = (TextView) rowView
						.findViewById(R.id_row_pacotes_contratados.txtview_nome_pacote);
				TextView txtViewValor = (TextView) rowView
						.findViewById(R.id_row_pacotes_contratados.txtview_valor);
				TextView txtViewValorDolar = (TextView) rowView
						.findViewById(R.id_row_pacotes_contratados.txtview_valor_dolar);
				// final EditText txtViewDesconto = (EditText)
				// rowView.findViewById(R.id_row_pacotes_contratados.edttex_desc);
				final TextView txtViewTotal = (TextView) rowView
						.findViewById(R.id_row_pacotes_contratados.txtview_total);
				txtViewTotal.setText("0");
				final EditText edtTextQuantidade = (EditText) rowView
						.findViewById(R.id_row_pacotes_contratados.edttex_quantidade);
				txtViewNome.setText(pacote.getNome());
				txtViewValor.setText("R$ " + pacote.getPreco().toString());
				txtViewValorDolar.setText("  $ "
						+ pacote.getPrecoDolar().toString());
				edtTextQuantidade.setText(pacote.getQuantidade() + "");
				edtTextQuantidade.addTextChangedListener(new TextWatcher() {
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
						if (count > 0) {
							Integer quant = Integer.valueOf(s.toString());
							Float preco = pacote.getPreco();
							if (act.tipoMoedaExibido == 1) { // Dolar
								preco = pacote.getPrecoDolar();
							}
							float tot = (preco * quant);
							txtViewTotal.setText("" + tot);
							pacote.setQuantidade(quant);
							act.updateTotais(listPacotesBean);
						}
					}

					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
					}

					public void afterTextChanged(Editable s) {
						if (!"".equalsIgnoreCase(s.toString())) {
							Integer quant = Integer.valueOf(s.toString());
							Float preco = pacote.getPreco();
							if (act.tipoMoedaExibido == 1) { // Dolar
								preco = pacote.getPrecoDolar();
							}
							float tot = (preco * quant);
							txtViewTotal.setText("" + tot);
							pacote.setQuantidade(quant);
							act.updateTotais(listPacotesBean);
						}
					}
				});

				if (act.tipoMoedaExibido == 0) { // real R$
					txtViewTotal.setText(pacote.getQuantidade()
							* pacote.getPreco() + "");
				} else { // Dolar
					txtViewTotal.setText(pacote.getQuantidade()
							* pacote.getPrecoDolar() + "");
				}

				// txtViewDesconto.addTextChangedListener(new TextWatcher() {
				// public void onTextChanged(CharSequence s, int start, int
				// before, int count) {
				// if (count > 0 && edtTextQuantidade.getText() != null &&
				// edtTextQuantidade.getText().length() > 0) {
				// Integer quantidade =
				// Integer.valueOf(edtTextQuantidade.getText().toString());
				// Integer desconto = Integer.valueOf(s.toString());
				// float tot = (pacote.getPreco() * quantidade);
				// txtViewTotal.setText("" + (tot - desconto));
				// }
				// }
				//
				// public void beforeTextChanged(CharSequence s, int start, int
				// count, int after) {
				// }
				//
				// public void afterTextChanged(Editable s) {
				// }
				// });
			}
		}
		return rowView;
	}
}
